package com.wlj.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wlj.dao.UserDao;
import com.wlj.dao.UserRoleDao;
import com.wlj.dto.UserMenuDTO;
import com.wlj.dto.UserResourcesDTO;
import com.wlj.dto.UserRoleDTO;
import com.wlj.exception.CustomException;
import com.wlj.form.UserAddForm;
import com.wlj.pojo.TbUser;
import com.wlj.pojo.TbUserRole;
import com.wlj.service.UserService;
import com.wlj.utils.CBeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;


@Service
public class UserServiceImpl extends ServiceImpl<UserDao, TbUser> implements UserService {

    private Logger logger= LoggerFactory.getLogger(getClass());

    @Autowired
    private UserDao userDao;

    @Autowired
    private UserRoleDao userRoleDao;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserResourcesDTO getUserAndResources(String username) {
        return userDao.getUserAndResources(username);
    }

    @Override
    public UserRoleDTO getUserAndRoles(String username) {
        return userDao.getUserAndRoles(username);
    }

    @Override
    public UserMenuDTO getUserAndMenus(String username) {
        return userDao.getUserAndMenus(username);
    }

    @Override
    public List<UserRoleDTO> listUsersAndRole() {
//        if(StringUtils.isBlank(lastTime)){
//            lastTime= DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now());
//        }
        List<UserRoleDTO> userRoleDTOS = userDao.listUsersAndRole();
        return userRoleDTOS;
    }

    @Override
    public UserRoleDTO getUserAndRoleById(Integer userId) {
        return userDao.getUserAndRoleById(userId);
    }

    @Override
    public TbUser findByUsername(String username) {
        return userDao.findByUsername(username);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addUser(UserAddForm userAddForm) {

        TbUser user = new TbUser();
        BeanUtils.copyProperties(userAddForm,user, CBeanUtils.getNullPropertyNames(userAddForm));
        user.setPassword(passwordEncoder.encode(userAddForm.getPassword()));
        userDao.addUser(user);
        String[] roles = userAddForm.getRoles();
        for (String role : roles) {
            TbUserRole tbUserRole = new TbUserRole();
            tbUserRole.setUserId(user.getId());
            tbUserRole.setRoleId(Integer.valueOf(role));
            userRoleDao.addUserRole(tbUserRole);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delUser(Integer userId) {
        userRoleDao.delUserRoleByUserId(userId);
        userDao.delUserByrId(userId);
    }


}
